﻿using System;

using umbraco.cms.businesslogic.datatype;
using umbraco.interfaces;

namespace Our.Umbraco.DataType.Bitly
{
	/// <summary>
	/// A bit.ly URL shortener data-type for Umbraco.
	/// </summary>
	public class BitlyDataType : AbstractDataEditor
	{
		/// <summary>
		/// The bit.ly URL shortener control.
		/// </summary>
		private BitlyControl m_Control = new BitlyControl();

		/// <summary>
		/// The PreValue Editor for the data-type.
		/// </summary>
		private BitlyPrevalueEditor m_PreValueEditor;

		/// <summary>
		/// Initializes a new instance of the <see cref="BitlyDataType"/> class.
		/// </summary>
		public BitlyDataType()
			: base()
		{
			// set the render control as the placeholder
			this.RenderControl = this.m_Control;

			// assign the initialise event for the placeholder
			this.m_Control.Init += new EventHandler(this.m_Control_Init);

			// assign the save event for the data-type/editor
			this.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(this.DataEditorControl_OnSave);
		}

		/// <summary>
		/// Gets the id of the data-type.
		/// </summary>
		/// <value>The id of the data-type.</value>
		public override Guid Id
		{
			get
			{
				return new Guid("751FAE59-5313-4A55-8A00-0344FBCD94B8");
			}
		}

		/// <summary>
		/// Gets the name of the data type.
		/// </summary>
		/// <value>The name of the data type.</value>
		public override string DataTypeName
		{
			get
			{
				return "Bit.ly Url Shortener";
			}
		}

		/// <summary>
		/// Gets the prevalue editor.
		/// </summary>
		/// <value>The prevalue editor.</value>
		public override IDataPrevalue PrevalueEditor
		{
			get
			{
				if (this.m_PreValueEditor == null)
				{
					this.m_PreValueEditor = new BitlyPrevalueEditor(this);
				}

				return this.m_PreValueEditor;
			}
		}

		/// <summary>
		/// Handles the Init event of the m_Control control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_Control_Init(object sender, EventArgs e)
		{
			// get the prevalue options
			this.m_Control.Options = ((BitlyPrevalueEditor)this.PrevalueEditor).GetPreValueOptions();

			// set the value of the control (not on PostBack)
			if (!this.m_Control.Page.IsPostBack)
			{
				this.m_Control.ShortUrl = this.Data.Value != null ? this.Data.Value.ToString() : string.Empty;
			}
		}

		/// <summary>
		/// Saves the editor control value.
		/// </summary>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void DataEditorControl_OnSave(EventArgs e)
		{
			if (this.m_Control.Options.AutoGenerate && string.IsNullOrEmpty(this.m_Control.ShortUrl))
			{
				this.m_Control.ShortUrl = Helper.GetBitlyUrl(this.m_Control.Options.BitlyUsername, this.m_Control.Options.BitlyApiKey, this.m_Control.LongUrl);
			}

			// save the value of the control
			this.Data.Value = this.m_Control.ShortUrl;
		}
	}
}
